package com.atguigu3;

import com.beans.Order;
import com.utils.JDBCUtils;
import org.junit.Test;

import java.lang.reflect.Field;
import java.sql.*;

/**
 * @auther user
 * @date 2021/12/7 - 1:59
 */
public class OrderForQueryTest {
    @Test
    public void orderForQuery() {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            //1 获取连接
            connection = JDBCUtils.getConnection();
            //2 预编译sql,返回preparedStatment实例
            String sql = "select order_id as orderId,order_name as orderName,order_date as orderDate from `order` where order_id = ?";
            preparedStatement = connection.prepareStatement(sql);
            //3 填充占位符
            preparedStatement.setObject(1,4);
            //4 执行，返回ResultSet结果集
            resultSet = preparedStatement.executeQuery();
            //5 处理结果集
            if (resultSet.next()){
                //获取列值
                int orderId = resultSet.getInt(1);
                String orderName = resultSet.getString(2);
                Date orderDate = resultSet.getDate(3);
                Order order = new Order(orderId, orderName, orderDate);
                System.out.println("order = " + order);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //6 关闭资源
            JDBCUtils.closeResource(connection,preparedStatement,resultSet);
        }
    }

    /**
     *  封装方法
     * @param sql
     * @param args
     * @return
     * @throws Exception
     */
    public Order orderForQuery1(String sql,Object ...args) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            //1 获取连接
            connection = JDBCUtils.getConnection();
            //2 预编译sql,返回preparedStatment实例
            preparedStatement = connection.prepareStatement(sql);
            //3 填充占位符
            for (int i = 0; i < args.length; i++) {
                preparedStatement.setObject(i+1,args[i]);
            }
            //4 执行，返回ResultSet结果集
            resultSet = preparedStatement.executeQuery();
            //5 处理结果集
            //获取元数据
            ResultSetMetaData metaData = resultSet.getMetaData();
            //获取总列数
            int columnCount = metaData.getColumnCount();
            if (resultSet.next()){
                Order order = new Order();
                for (int i = 0; i < columnCount; i++) {
                    // metaData.getCatalogName(i+1) 获取列名，但是有时列名和JavaBean属性名不相同
                    //获取列的别名
                    String columnLabel = metaData.getColumnLabel(i + 1);
                    //获取列的值
                    Object columnValue = resultSet.getObject(i + 1);
                    //通过反射，将列值columnValue存储到Order对象的columnLabel属性
                    Field field = Order.class.getDeclaredField(columnLabel);
                    field.setAccessible(true);
                    field.set(order,columnValue);
                }
                return order;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //6 关闭资源
            JDBCUtils.closeResource(connection,preparedStatement,resultSet);
        }
        return null;
    }
    @Test
    public void testOrderForQuery1(){
        String sql = "select order_id as orderId,order_name as orderName,order_date as orderDate from `order` where order_id = ?";
        Order order = orderForQuery1(sql, 4);
        System.out.println("order = " + order);
    }
}
